<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2024/12/23
 * Time: 16:10
 */

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Routing\Controller as BaseController;




class TestController extends Controller
{
    public function test(Request $request)
    {
//        \PhpOffice\PhpWord\Settings::setZipClass(\PhpOffice\PhpWord\Settings::PCLZIP);
//        \PhpOffice\PhpWord\Settings::setZipClass(\PhpOffice\PhpWord\Settings::PCLZIP);
        $templateProcessor = new \PhpOffice\PhpWord\TemplateProcessor("record.docx");

//        return 333;
//        $templateProcessor->addText('Hello ${name}!');
        $templateProcessor->setValue('name', $request->name);
        $templateProcessor->setValue('sex', $request->sex);
        $templateProcessor->setValue('age', $request->age);


        //创建文件名
        $filename = date("YmdHis") . ".docx";
//保存word  存放位置可以自定义
        $templateProcessor->saveAs(public_path("uploads/doc/" . $filename));
        return 222;
    }
    public function index(Request $request){

//        $inquiry = '';
//        $answer = '';


//        return $request;

        $templateProcessor = new \PhpOffice\PhpWord\TemplateProcessor("record.docx");

//        return 333;
//        $templateProcessor->addText('Hello ${name}!');
        $templateProcessor->setValue('name', $request->name);
        $templateProcessor->setValue('sex', $request->sex);
        $templateProcessor->setValue('age', $request->age);
        $templateProcessor->setValues(array(
            'phone' => $request->phone,
            'address' =>  $request->address,
//            'ID-NO' => $request->ID-NO,
//            'inquiry-address' =>  $request->inquiry-address,
//            'inquiry-person' => $request->inquiry-person,
//            'recorder' =>  $request->recorder
        ));
        $templateProcessor->setValue('ID-NO', $request->ID_NO);
        $templateProcessor->setValue('inquiry-address', $request->inquiry_address);
        $templateProcessor->setValue('inquiry-person', $request->inquiry_person);
        $templateProcessor->setValue('recorder', $request->recorder);
        $templateProcessor->setValue('place', $request->place);
        $templateProcessor->setValue('answer', $request->answer);
        $templateProcessor->setValue('answer2', $request->answer2);
        $templateProcessor->setValue('answer3', $request->answer3);

        $templateProcessor->setValue('yer', substr($request->date,0,4));
        $templateProcessor->setValue('month', substr($request->date,5,2));
        $templateProcessor->setValue('day', substr($request->date,8,2));
        $templateProcessor->setValue('hour', substr($request->date,11,2));
        $templateProcessor->setValue('min', substr($request->date,14,2));
        $templateProcessor->setValue('over-hour', substr($request->overdate,11,2));
        $templateProcessor->setValue('over-min', substr($request->overdate,14,2));
        $inquiry = '';
        $answer = '';
        $inline =new \PhpOffice\PhpWord\Element\TextRun();

        foreach ($request->text as $value){
//            return $value['inquiry'];
            $inquiry = $inquiry. $value['inquiry'].':'. $answer. $value['answer'].'<w:br/>'.'     ';
//            $answer = $answer. $value['answer'];
//            $inline->addTextBreak();
//            $inline->addText($answer.'\n', array('paragraph' => array('spacing' => 200)));
        }
        $inline->addText($inquiry,array('name'=>'仿宋','color'=>'#ff0000','size'=>12));

//        return $inquiry;
        $templateProcessor->setComplexValue('text', $inline);



        //创建文件名
        $filename = date("YmdHis") . ".docx";
//保存word  存放位置可以自定义
        $templateProcessor->saveAs(public_path("uploads/doc/" . $filename));
        return $request;
    }
}